# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=transmission
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cli"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gtk"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt")
pkgver=3.00
pkgrel=5
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.transmissionbt.com/'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libb64"
         "${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-libnatpmp"
         "${MINGW_PACKAGE_PREFIX}-miniupnpc"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-dht"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-libutp"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-qt5-winextras"
             "intltool")
source=("https://github.com/transmission/transmission-releases/raw/master/transmission-${pkgver}.tar.xz"
        "https://raw.githubusercontent.com/transmission/transmission/${pkgver}/po/pt_PT.po" # missing in tarball
        "001-transmission-3.00-openssl-3.patch")
sha256sums=('9144652fe742f7f7dd6657716e378da60b751aaeda8bef8344b3eefc4db255f2'
            'f96b4b03d977fadca51716bcecc51582c980a7d5f1e1519b97837955f5fae59e'
            'a5e56b906724f007db0bdb9835fbf5088bb56a521ec2971aec0ea44578d5955b')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-transmission-3.00-openssl-3.patch

  cp "${srcdir}/pt_PT.po" po/

  sed -i '/^Icon=/ s/$/-qt/' qt/transmission-qt.desktop
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # This icon is missing in the tarball, and does not get copied to the build directory,
  # but we can borrow the Qt one as it's identical
  mkdir gtk
  cp "${srcdir}/${_realname}-${pkgver}"/qt/qtr.ico gtk/transmission.ico

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_TESTS=OFF \
      -DENABLE_QT=ON \
      -DENABLE_GTK=ON \
      -DENABLE_CLI=ON \
      -DUSE_SYSTEM_EVENT2=ON \
      -DUSE_SYSTEM_DHT=ON \
      -DUSE_SYSTEM_MINIUPNPC=ON \
      -DUSE_SYSTEM_NATPMP=ON \
      -DUSE_SYSTEM_UTP=ON \
      -DUSE_SYSTEM_B64=ON \
      -DWITH_CRYPTO=openssl \
      -DINSTALL_DOC=ON \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package_transmission-cli() {
  pkgdesc="Fast, easy, and free BitTorrent client - CLI tools, daemon and web client (mingw-w64)"

  for dir in daemon cli utils; do
    cd "${srcdir}/build-${MSYSTEM}/${dir}"

    DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  done

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-cli/COPYING"
}

package_transmission-gtk() {
  pkgdesc="Fast, easy, and free BitTorrent client - GTK+ GUI (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-gtk3"
            "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme")
  optdepends=("${MINGW_PACKAGE_PREFIX}-libnotify: Desktop notification support"
              "${MINGW_PACKAGE_PREFIX}-transmission-cli: daemon and web support")
  replaces=("${MINGW_PACKAGE_PREFIX}-transmission")
  conflicts=("${MINGW_PACKAGE_PREFIX}-transmission")
  provides=("${MINGW_PACKAGE_PREFIX}-transmission")

  for dir in gtk po; do
    cd "${srcdir}/build-${MSYSTEM}/${dir}"

    DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  done

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 gtk/transmission.png "${pkgdir}${MINGW_PREFIX}/share/pixmaps/transmission.png"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-gtk/COPYING"
}

package_transmission-qt() {
  pkgdesc='Fast, easy, and free BitTorrent client - Qt GUI (mingw-w64)'
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base"
            "${MINGW_PACKAGE_PREFIX}-qt5-winextras")
  optdepends=("${MINGW_PACKAGE_PREFIX}-transmission-cli: daemon and web support")

  cd "${srcdir}/build-${MSYSTEM}/qt"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  rm -rf "${pkgdir}${MINGW_PREFIX}/etc"

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 qt/icons/transmission.png "${pkgdir}${MINGW_PREFIX}/share/pixmaps/transmission-qt.png"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licences/transmission-qt/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
